angular.module('withdraw-cash-form',[])

.controller 'withdrawCashFormCtrl', ($scope,showToast,$http,userData,$ionicHistory) ->
    loadData=()->
        $http.get '/accounts',params:{action:'getallcash',userid:userData.getToken()}
        .success (resp) ->
            if resp.status == '0'
                $scope.balance=Number(resp.allcash) if Number(resp.allcash)>=0
        .error (err)->
            console.log err
    $scope.$on '$ionicView.enter',->
        loadData()
        $scope.reqData={
            cashmoney:''
            bankname:''
            bankcard:''
            bankuser:''
            aliaccount:''
        }
    _.extend $scope,
        balance:''
        cashType:'card'
        inputAmt:''
        reqData:
            cashmoney:''
            bankname:''
            bankcard:''
            bankuser:''
            aliaccount:''
        goBack: ->
            $ionicHistory.goBack()
        checkAmount:()->
            reg=/^([1-9][\d]{0,8})$/;
            money=$scope.reqData.cashmoney
            if money=='' or money==undefined
                money=''
            else if reg.test(money)
                angular.noop
            else
                money=money.substr(0,money.length-1)
            $scope.reqData.cashmoney=money
        changeType:()->
            if $scope.cashType=='card'
                $scope.cashType='alipay'
#                $scope.reqData.bankcard=''
#                $scope.reqData.bankname=''
#                $scope.reqData.bankuser=''
            else
                $scope.cashType='card'
#                $scope.reqData.aliaccount=''
        checkForm:()->
            data=$scope.reqData
            if data.cashmoney==''
                showToast '提现金额不能为空'
                return false
            if Number(data.cashmoney)>Number($scope.balance)
                showToast '提现金额不能大于可提金额'
                return false
            if $scope.cashType=='card' and (data.bankname=='' or data.bankuser=='' or data.bankcard=='')
                showToast '选择银行卡提现,银行信息不能为空'
                return false
            else if $scope.cashType=='alipay' and data.aliaccount==''
                showToast '选择支付宝提现,支付宝信息不能为空'
                return false
            return true
        submit:()->
            if !$scope.checkForm()
                return
            $scope.reqData.action='supplycash'
            $scope.reqData.userid=userData.getToken()
            if $scope.cashType!='card'
                $scope.reqData.bankcard=''
                $scope.reqData.bankname=''
                $scope.reqData.bankuser=''
            else
                $scope.reqData.aliaccount=''
            $http.post '/utf-8data/',$scope.reqData
            .success (resp) ->
                if resp.status =='0'
                    showToast '提现申请成功'
                    $ionicHistory.goBack()
                else
                    showToast resp.msg
            .error (err) ->
                showToast '网络连接不稳定!'